package com.ruoyi.web.controller.market.special;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.db.domain.SpecialCostomInfo;
import com.ruoyi.db.domain.SpecialCostomItemInfo;
import com.ruoyi.special.service.ISpecialCostomInfoService;
import com.ruoyi.special.service.ISpecialCostomItemInfoService;
import com.ruoyi.special.service.ISpecialDataInfoService;
import com.ruoyi.special.service.ISpecialMemberService;
import com.ruoyi.web.service.IMemberInfoHandleService;
import com.ruoyi.web.wapper.SpecialDataInfoWapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 渠道信息Controller
 * 
 * @author ruoyi
 * @date 2022-08-04
 */
@Controller
@RequestMapping("/special/agency/task")
public class SpecialAgencyTaskController extends BaseController
{
    private String prefix = "special/agency/task";

    @Autowired
    ISpecialMemberService specialMemberService;
    @Autowired
    ISpecialCostomInfoService specialCostomInfoService;
    @Autowired
    IMemberInfoHandleService memberInfoHandleService;
    @Autowired
    ISpecialDataInfoService specialDataInfoService;
    @Autowired
    ISpecialCostomItemInfoService specialCostomItemInfoService;

    @RequiresPermissions("special:agency:task")
    @GetMapping("")
    public String view(ModelMap mmap)
    {
        BigDecimal balanceAmount = BigDecimal.ZERO;
        SysUser sysUser = getSysUser();
        if (!sysUser.isAdmin()){
            // 设置，查询
            SpecialCostomInfo costomInfo = specialCostomInfoService.getAccountInfoByDeptId(sysUser.getDeptId());
            mmap.put("balanceAmount", costomInfo.getRemainAmount());
        }else{
            mmap.put("balanceAmount", balanceAmount);
        }

        return prefix + "/list";
    }

    /**
     * 任务列表
     * @param specialCostomItemInfo
     * @return
     */
    @RequiresPermissions("special:agency:task")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SpecialCostomItemInfo specialCostomItemInfo)
    {

        SysUser sysUser = getSysUser();
        if (!sysUser.isAdmin()){
            // 设置，查询
            specialCostomItemInfo.setDeptId(sysUser.getDeptId());
        }

        // 查询用户列表数据
        startPage();
        List<Map<String, Object>> mapList = specialCostomItemInfoService.selectSpecialCostomItemInfoListMap(specialCostomItemInfo);
        mapList = new SpecialDataInfoWapper(mapList).wrap();

        TableDataInfo tableDataInfo = getDataTable(mapList);
        return tableDataInfo;
    }

    @RequiresPermissions("special:agency:task")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(SpecialCostomItemInfo specialCostomItemInfo)
    {
        return toAjax(specialCostomItemInfoService.updateSpecialCostomItemInfo(specialCostomItemInfo));
    }

    /**
     * 账户余额信息
     * @param deptId
     * @return
     */
    @RequiresPermissions("special:agency:task")
    @PostMapping("/accountInfo")
    @ResponseBody
    public AjaxResult getAccountInfo(@RequestParam(value = "deptId", required = false) Long deptId)
    {
        JSONObject returnData = new JSONObject();
        SysUser user = getSysUser();
        if (user.isAdmin() && ObjectUtil.isEmpty(deptId)){
            returnData.put("show", false);
            return AjaxResult.success(returnData);
        }

        if (ObjectUtil.isEmpty(deptId)){
            deptId = user.getDeptId();
        }

        SpecialCostomInfo costomInfo = specialCostomInfoService.getAccountInfoByDeptId(deptId);
        if (ObjectUtil.isEmpty(costomInfo)){
            return AjaxResult.error();
        }

        returnData.put("remainAmount", costomInfo.getRemainAmount());
        returnData.put("show", true);
        return AjaxResult.success(returnData);
    }

}
